package com.songxh.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.songxh.pojo.Admin;
import com.songxh.pojo.Grade;
import com.songxh.service.AdminService;
import com.songxh.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author songxh
 * @create 2022-04-18-19:36
 */
@Api("Admin控制器")
@RestController
@RequestMapping("/sms/adminController")
public class AdminController {

    @Autowired
    private AdminService adminService;


    @ApiOperation("获取管理员信息")
    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    public Result getAllAdmin(
            @ApiParam("页码")
            @PathVariable("pageNo")Integer pageNo,
            @ApiParam("每一页的页数")
            @PathVariable("pageSize")Integer pageSize,
            String adminName
    ){
        //分页
        Page<Admin> page=new Page<>(pageNo,pageSize);
        //通过服务层来操作
        IPage<Admin> pageRes=adminService.getAdminByOpr(page,adminName);

        return Result.ok(pageRes);
    }


    @ApiOperation("添加或者修改管理员信息")
    @PostMapping("/saveOrUpdateAdmin")
    public Result saveOrUpdateAdmin(@ApiParam("该Admin信息的JSON内容")@RequestBody Admin admin){
        //实现服务层的增加与修改操作
        //该方法在IService中已经存在，故而直接用即可
        adminService.saveOrUpdate(admin);
        return Result.ok();
    }


    @ApiOperation("删除管理员信息")
    @DeleteMapping("/deleteAdmin")
    public Result deleteAdmin(@ApiParam("需要删除的Admin的id值的JSON集合")@RequestBody List<Integer> ids){
        // .removeByIds(ids)该方法来自IService，由mybatisplus提供
        adminService.removeByIds(ids);
        return Result.ok();
    }


}
